package com.situ.user.mapper;

import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import com.situ.user.bean.Goods;
@Mapper
public interface GoodsMapper {
	@Delete({ "delete from goods", "where id = #{id,jdbcType=INTEGER}" })
	int deleteByPrimaryKey(Integer id);

	@Insert({ "insert into goods ( goods_sort_id, ", "name, dscp, price, ", "version, color, ", "suit, count)",
			"values ( #{goodsSortId,jdbcType=INTEGER}, ",
			"#{name,jdbcType=VARCHAR}, #{dscp,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL}, ",
			"#{version,jdbcType=VARCHAR}, #{color,jdbcType=VARCHAR}, ",
			"#{suit,jdbcType=VARCHAR}, #{count,jdbcType=INTEGER} )" })
	@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
	int insert(Goods record);

	@Select({ "select", "id, goods_sort_id, name, dscp, price, version, color, suit, count, status", "from goods",
			"where id = #{id,jdbcType=INTEGER}" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "goods_sort_id", property = "goodsSortId", jdbcType = JdbcType.INTEGER),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "dscp", property = "dscp", jdbcType = JdbcType.VARCHAR),
			@Result(column = "price", property = "price", jdbcType = JdbcType.DECIMAL),
			@Result(column = "version", property = "version", jdbcType = JdbcType.VARCHAR),
			@Result(column = "color", property = "color", jdbcType = JdbcType.VARCHAR),
			@Result(column = "suit", property = "suit", jdbcType = JdbcType.VARCHAR),
			@Result(column = "count", property = "count", jdbcType = JdbcType.INTEGER),
			@Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
			@Result(column = "id", property = "pictures", many = @Many(select = "com.situ.user.mapper.GoodsPicturreMapper.selectByGoodsId")) })
	Goods selectByPrimaryKey(Integer id);

	@Select({ "select", "id, goods_sort_id, name, dscp, price, version, color, suit, count, status", "from goods" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "goods_sort_id", property = "goodsSortId", jdbcType = JdbcType.INTEGER),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "dscp", property = "dscp", jdbcType = JdbcType.VARCHAR),
			@Result(column = "price", property = "price", jdbcType = JdbcType.DECIMAL),
			@Result(column = "version", property = "version", jdbcType = JdbcType.VARCHAR),
			@Result(column = "color", property = "color", jdbcType = JdbcType.VARCHAR),
			@Result(column = "suit", property = "suit", jdbcType = JdbcType.VARCHAR),
			@Result(column = "count", property = "count", jdbcType = JdbcType.INTEGER),
			@Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER) })
	List<Goods> selectAll();

	@Update({ "<script>", "update goods", "<set>",
			"<if test = 'goodsSortId!=null'> goods_sort_id = #{goodsSortId}, </if>",
			"<if test = 'name!=null'> name = #{name}, </if>", "<if test = 'dscp!=null'> dscp = #{dscp}, </if>",
			"<if test = 'price!=null'> price = #{price}, </if>",
			"<if test = 'version!=null'> version = #{version}, </if>",
			"<if test = 'color!=null'> color = #{color}, </if>", "<if test = 'suit!=null'> suit = #{suit}, </if>",
			"<if test = 'count!=null'> count = #{count}, </if>", "<if test = 'status!=null'> status = #{status}, </if>",
			"</set>", "where id = #{id}", "</script>" })
	int updateByPrimaryKey(Goods record);

	@Select({ "<script>", "select * from goods", "INNER JOIN goods_sort on goods.goods_sort_id = goods_sort.id",
			"<where>",
			"<if test = 'goods.goodsSortId !=null and goods.goodsSortId != 0'> goods_sort_id = #{goods.goodsSortId}</if>",
			"<if test = 'goods.name !=null'> and goods.name like '%${goods.name}%'</if>",
			"<if test = 'goods.price !=null'> and price =#{goods.price}</if>",
			"<if test = 'goods.status !=null'> and goods.status =#{goods.status}</if>",
			"<if test = 'firstSortId != null and firstSortId != 0'> and parent = #{firstSortId}</if>", "</where>",
			"</script>" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "goods_sort_id", property = "goodsSortId", jdbcType = JdbcType.INTEGER),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "dscp", property = "dscp", jdbcType = JdbcType.VARCHAR),
			@Result(column = "price", property = "price", jdbcType = JdbcType.DECIMAL),
			@Result(column = "version", property = "version", jdbcType = JdbcType.VARCHAR),
			@Result(column = "color", property = "color", jdbcType = JdbcType.VARCHAR),
			@Result(column = "suit", property = "suit", jdbcType = JdbcType.VARCHAR),
			@Result(column = "count", property = "count", jdbcType = JdbcType.INTEGER),
			@Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
			@Result(column = "goods_sort_id", property = "goodsSort", one = @One(select = "com.situ.user.mapper.GoodsSortMapper.selectByPrimaryKey")),
			@Result(column = "id", property = "pictures", many = @Many(select = "com.situ.user.mapper.GoodsPicturreMapper.selectByGoodsId")) })
	List<Goods> select(@Param("goods") Goods goods, @Param("firstSortId") Integer firstSortId);
}